package csc.com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name="ChangePassWord", urlPatterns = { "/changePassword" })
public class ChangePasswordServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 411671708263896021L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String user = req.getParameter("username");
		String pass = req.getParameter("password");
		Connection con = (Connection) getServletContext().getAttribute("DBConnection");
		PreparedStatement ps = null;
		try {
			ps = con.prepareStatement("update usermanagement.users set password='"+ pass + "' where username='"+user+"'");
			ps.execute();
			RequestDispatcher rd = getServletContext().getRequestDispatcher("/loginSuccess.html");
			PrintWriter out = resp.getWriter();
			out.println("<font color=green>Hello ,"+ user + "</font><br/>");
			rd.include(req, resp);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new ServletException("DB Connection problem.");
		} finally {
			try {
				ps.close();
			} catch (SQLException e) {
			}
		}
	}
	
}
